//
//  QLPeopleNearbyVC.m
//  神约
//
//  Created by qlwl on 17/1/23.
//  Copyright © 2017年 qlwl. All rights reserved.
//

#import "QLPeopleNearbyVC.h"
#import "QLNearbyPeopleModel.h"
#import "QLNearbyPeopleCollCell.h"

#define ItemSpace (8)
#define CellWidth(num) (iphoneWidth - (num + 1)*ItemSpace)/num
#define CellHeight(num) (CellWidth(num))

@interface QLPeopleNearbyVC ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation QLPeopleNearbyVC
{
    int _imaNum;
    NSArray * _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    ((UICollectionViewFlowLayout * )self.collectionView.collectionViewLayout).minimumLineSpacing = ItemSpace;
    ((UICollectionViewFlowLayout * )self.collectionView.collectionViewLayout).minimumInteritemSpacing = ItemSpace;
    _imaNum = 2;
    [self netWorking];
}
- (void)reloadIma
{
    if (_imaNum == 2) {
        _imaNum = 3;
    }else{
        _imaNum = 2;
    }
    [self.collectionView reloadData];
}

#pragma mark CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    QLNearbyPeopleCollCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QLNearbyPeopleCollCell" forIndexPath:indexPath];
    QLNearbyPeopleModel * model = _dataArray[indexPath.row];
    [cell.userIma QL_setImageWithURL:[NSString stringWithFormat:@"%@%@",HTTP_BaseURL,model.idbackurl] placeholderImage:nil];
    cell.nameLab.text = model.name;
    cell.sexIma.image = nil;
    cell.ageLab.text = model.age;
    cell.distanceLab.text = [NSString stringWithFormat:@"%.0fkm",model.distince];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(CellWidth(_imaNum), CellHeight(_imaNum));
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(ItemSpace,ItemSpace,ItemSpace,ItemSpace);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    return YES;
}

- (void) netWorking
{
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"loginid"] = @"2";
    [QLNetWorking postJSONWithUrl:QLFindNearbyPeopleURL parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        if ([QLNetWorking sucess:[result objectForKey:@"mobileHead"] and:@"code" EqualToString:@"success"]) {
            _dataArray = [QLNearbyPeopleModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"mobileBody"] objectForKey:@"data"]];
            [self.collectionView reloadData];
        }
        NSLog(@"%@",result);
    } fail:^(NSError *error) {
        
    }];
}

@end
